package cn.lj.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.lj.domain.Appointment;
import cn.lj.domain.Book;
import cn.lj.domain.Student;
import cn.lj.dto.AppointExecution;
import cn.lj.dto.Result;
import cn.lj.exception.NoNumberException;
import cn.lj.exception.RepeatAppointException;
import cn.lj.service.BookService;
import cn.lj.util.AppointStateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 获取图书列表
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    private String List(Model model){
        List<Book> list = bookService.findAll();
        model.addAttribute("list", list);
        return "list";
    }

    /**
     * 搜索是否有某图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    private void  search(HttpServletRequest req,HttpServletResponse resp)
            throws ServletException, IOException{
        //接收页面的值
        String name=req.getParameter("name");
        name=name.trim();
        //向页面传值
        req.setAttribute("name", name);
        req.setAttribute("list", bookService.findListByName(name));
        req.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(req, resp);
    }

    /**
     * 查看某图书的详细情况
     * @param bookId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{bookId}/detail", method = RequestMethod.GET)
    private String detail(@PathVariable("bookId") Long bookId, Model model){
        if(bookId==null){
            return "redirect:/book/list";
        }
        Book book=bookService.findById(bookId);
        if(book==null){
            return "forward:/book/list";
        }
        model.addAttribute("book",book);
        System.out.println(book);
        return "detail";
    }

    /**
     * 验证输入的用户名、密码是否正确
     * @param studentId
     * @param password
     * @return
     */
    @RequestMapping(value="/verify", method = RequestMethod.POST, produces = {
            "application/json; charset=utf-8" })
    @ResponseBody
    private Map validate(Long studentId,Long password){   //(HttpServletRequest req,HttpServletResponse resp){
        Map resultMap=new HashMap();
        Student student;
        System.out.println("验证函数");
        student =bookService.checkStudent(studentId,password);

        System.out.println("输入的学号、密码："+studentId+":"+password);
        System.out.println("查询到的："+student.getStudentId()+":"+student.getPassword());

        if(student!=null){
            System.out.println("SUCCESS");
            resultMap.put("result", "SUCCESS");
        }else{
            resultMap.put("result", "FAILED");
        }
        return resultMap;

    }

    /**
     * 执行预约的逻辑
     * @param bookId
     * @param studentId
     * @return
     */
    @RequestMapping(value = "/{bookId}/appoint", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    private Result<AppointExecution> execute(@PathVariable("bookId") Long bookId, @RequestParam("studentId") Long studentId){
        Result<AppointExecution> result;
        AppointExecution execution;

        try{//手动try catch,在调用appoint方法时可能报错
            execution=bookService.appoint(bookId, studentId);
            result= new Result<>(true, execution);
            return result;

        } catch(NoNumberException e1) {
            execution=new AppointExecution(bookId, AppointStateEnum.NO_NUMBER);
            result= new Result<>(true, execution);
            return result;
        }catch(RepeatAppointException e2){
            execution=new AppointExecution(bookId,AppointStateEnum.REPEAT_APPOINT);
            result= new Result<>(true, execution);
            return result;
        }catch (Exception e){
            execution=new AppointExecution(bookId,AppointStateEnum.INNER_ERROR);
            result= new Result<>(true, execution);
            return result;
        }
    }

    @RequestMapping(value ="/appoint")
    private String appointBooks(@RequestParam("studentId") long studentId,Model model){

        List<Appointment> appointList;
        appointList=bookService.findAppointByStu(studentId);
        model.addAttribute("appointList", appointList);

        return "appointBookList";
    }

}
